aboutsummaryrefslogtreecommitdiffstats
path: root/web/app/api/v1/links/[linkId]/route.ts
blob: 39449d6d971f0cc0af766331ca5f841afb1c6b2d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { authOptions } from "@/lib/auth";
import { unbookmarkLink } from "@/lib/services/links";
import { Prisma } from "@remember/db";

import { getServerSession } from "next-auth";
import { NextRequest } from "next/server";

export async function DELETE(
  _request: NextRequest,
  { params }: { params: { linkId: string } },
) {
  // TODO: We probably should be using an API key here instead of the session;
  const session = await getServerSession(authOptions);
  if (!session) {
    return new Response(null, { status: 401 });
  }

  try {
    await unbookmarkLink(params.linkId, session.user.id);
  } catch (e: unknown) {
    if (
      e instanceof Prisma.PrismaClientKnownRequestError &&
      e.code === "P2025" // RecordNotFound
    ) {
      return new Response(null, { status: 404 });
    } else {
      throw e;
    }
  }

  return new Response(null, { status: 201 });
}